<?php 
header("Content-type: text/html; charset=utf-8");

require_once('auth_i.inc.php');

$show_info = "";

if (!empty($_FILES) && ($_FILES['file']['error'] == 0) && (is_uploaded_file($_FILES['file']['tmp_name'])))
{
    if (isset($_POST['phase']) && isset($_POST['cont']))
    {
        $info = getimagesize($_FILES['file']['tmp_name']);

        if (($info[0] == 530) || ($info[0] == 320))
        {
            if ($info[2] == 3)       // PNG
            {
                $sth = $dbh->prepare('delete from pictures where c_id=:cid and phase=:phase');
                $sth->bindParam(':cid', intval($_POST['cont']), PDO::PARAM_INT);
                $sth->bindParam(':phase', intval($_POST['phase']), PDO::PARAM_INT);
                $sth->execute();

                $image = file_get_contents($_FILES['file']['tmp_name']);

                $sth = $dbh->prepare('insert into pictures (c_id, phase, image) values (:cid, :phase, :image)');
                $sth->bindParam(':cid', intval($_POST['cont']), PDO::PARAM_INT);
                $sth->bindParam(':phase', intval($_POST['phase']), PDO::PARAM_INT);
                $sth->bindParam(':image', base64_encode($image));
                $sth->execute();

                // success !

            }
            else
            {
                $show_info = "Неправильный формат картинки !\nДолжен быть .png";
            }
        }
        else
        {
            $show_info = "Неправильное разрешение картинки !\nДолжно быть 530x320";
        }

    }
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>

<head>
    <title>Управление сигнальными планами.</title>
    <?php
        require_once('headers.inc.php');
    ?>
    <script src="plans.js" type="text/javascript"></script>

    <script type="text/javascript">
<?php
    if (!empty($show_info))
    {
        echo 'alert("'. $show_info .'");'; 
    }

    if (isset($_POST['phase']) && isset($_POST['cont']))
    {
        echo 'var sel_phase='.$_POST['phase'].";\nvar sel_cont=".$_POST['cont'].";\n";
    }

    if (isset($_POST['scrollpos']))
    {
        echo 'var scrollpos="'.$_POST['scrollpos']."\"\n";
    }
?>
    </script>

</head>

<body>


<?php // menu bar
    require_once('menu.inc.php');

    print menu($menu_items, 'plans.php');
?>

<div id="upload-form" title="Загрузить новое изображение.">
    <form id="post-form" enctype="multipart/form-data" action="plans.php" method="post" class="FormGrid">

          <table id="plan-form-table">
          <tr>
          <td id="label">
            <label for="file">Картинка (только PNG 360x216):</label>
          </td>
          <td id="data">
            <input type="file" name="file" id="file" value=""/>
          </td>
          </tr>
          </table>

          <input type="hidden" name="phase" value=""/>
          <input type="hidden" name="cont" value=""/>
          <input type="hidden" name="scrollpos" value=""/>    
    </form>
</div>

<div id="right-info" >
        <button id="upload-button">Загрузить</button>
        <button id="remove-button">Удалить</button> 
</div>

<div id="content-plan">
</div>

<div id="bottom-plan">

<img name="preview" src="img/noimage.png" />
</div>

</body>
</html>